package com.tengju.user.infrastructure.repository.club;

import com.tengju.user.domain.model.club.UserZoneMemberInfo;
import com.tengju.user.domain.model.club.UserZoneMemberRepository;
import com.tengju.user.domain.model.club.ZoneId;
import com.tengju.user.domain.model.user.UserId;
import com.tengju.user.domain.model.user.UserIdCode;
import com.tengju.user.domain.model.user.UserInfo;
import com.tengju.user.infrastructure.persistence.mybatis.dataobject.UserZoneMemberDO;
import com.tengju.user.infrastructure.persistence.mybatis.mapper.dealer.custom.UserZoneMemberMapper;
import com.tengju.user.infrastructure.repository.club.convert.UserZoneRelationConvert;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Slf4j
@Repository
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class UserZoneMemberRepositoryImpl implements UserZoneMemberRepository {

    private final UserZoneMemberMapper userZoneMemberMapper;


    @Override
    public List<UserZoneMemberInfo> selectByZoneId(ZoneId zoneId) {
        List<UserZoneMemberDO> userZoneMemberDOList =
                userZoneMemberMapper.selectByZoneId(zoneId.getValue());
        return userZoneMemberDOList.stream()
                .map(UserZoneRelationConvert::toModel)
                .filter(Objects::nonNull).collect(Collectors.toList());
    }

    @Override
    public void deleteByZoneId(ZoneId zoneId) {
        userZoneMemberMapper.deleteByZoneId(zoneId.getValue());

    }

    @Override
    public void insert(ZoneId zoneId, UserIdCode userIdCode, UserId userId) {

        userZoneMemberMapper.insert(zoneId.getValue(),
                userIdCode.getValue(),
                userId.getValue());

    }

    @Override
    public void batchInsert(List<UserZoneMemberInfo> userZoneMemberInfoList) {

        if (CollectionUtils.isEmpty(userZoneMemberInfoList)) {
            return;
        }
        List<UserZoneMemberDO> userZoneMemberDOList =
                userZoneMemberInfoList.stream()
                        .map(UserZoneRelationConvert::modelToDO)
                        .filter(Objects::nonNull)
                        .collect(Collectors.toList());

        userZoneMemberMapper.batchInsert(userZoneMemberDOList);

    }

    @Override
    public List<UserIdCode> selectIdcodeByZoneId(ZoneId zoneId) {
        List<Long> userIdCodeList = userZoneMemberMapper.selectIdcodeByZoneId(zoneId.getValue());
        return userIdCodeList.stream()
                .filter(Objects::nonNull)
                .map(UserIdCode::new)
                .collect(Collectors.toList());
    }

    @Override
    public List<UserZoneMemberInfo> selectByUserIdCode(List<UserIdCode> userIdCodeList) {
        if (CollectionUtils.isEmpty(userIdCodeList)) {
            return Collections.emptyList();
        }
        List<Long> memberIdCodeList = userIdCodeList.stream()
                .filter(Objects::nonNull)
                .map(UserIdCode::getValue)
                .collect(Collectors.toList());

        if (CollectionUtils.isEmpty(memberIdCodeList)) {
            return Collections.emptyList();
        }

        List<UserZoneMemberDO> userZoneMemberDOList =
                userZoneMemberMapper.selectByUserIdCode(memberIdCodeList);
        return userZoneMemberDOList.stream()
                .map(UserZoneRelationConvert::toModel)
                .filter(Objects::nonNull).collect(Collectors.toList());

    }
}
